package com.zhu.service.edu.controller.admin;


import com.zhu.commons.utils.R;
import com.zhu.service.edu.feign.AliyunVodFeign;
import com.zhu.service.edu.pojo.EduVideo;
import com.zhu.service.edu.service.IEduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@RestController
@RequestMapping("admin/edu/video")
//@CrossOrigin
@Slf4j
@Api(tags = "章节->课时管理模块")
public class EduVideoController {
    @Autowired
    private IEduVideoService videoService;

   @ApiOperation(value = "添加课时")
    @PostMapping("/add")
    public R addVideo(@RequestBody EduVideo video){
       if(videoService.save(video)){
           return R.ok().message("添加成功！");
       }
       return R.error().message("添加失败");
   }

    @ApiOperation(value = "根据id查询课时")
    @GetMapping("/getVideoById/{id}")
    public R getVideoById(@PathVariable(value = "id")  String id){
        EduVideo eduVideo = videoService.getById(id);
        if(eduVideo!=null){
        return R.ok().data("item",eduVideo);
        }
        return R.error().message("课时不存在");
    }

    @ApiOperation(value = "根据id修改课时")
    @PutMapping("/UpdateVideoById")
    public R UpdateVideoById(@RequestBody  EduVideo video){
       if(videoService.updateById(video)){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }
   @Autowired
   private AliyunVodFeign vodFeign;
    @ApiOperation(value = "删除课时")
    @DeleteMapping("/deleteVideoById/{id}")
    public R deleteVideoById(@PathVariable("id")  String id) throws Exception {
        //删除阿里云视频
        EduVideo video = videoService.getById(id);
        vodFeign.delete(video.getVideoSourceId());
        log.info("阿里云视频删除成功");
        if(videoService.removeById(id)){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }
}
